#include "hal_function.h"
#include "oled.h"
#include "cynfsm.h"
#include "start.h"
#include "oled_mgr.h"
static StartHandle *startHanlde;


uint32_t startPage_Entry(uint32_t arg)
{
    startHanlde = malloc(sizeof(StartHandle));
    memset(startHanlde,0,sizeof(StartHandle));

    startHanlde->resources.screen = lv_obj_create(NULL);

    lv_obj_remove_style_all(startHanlde->resources.screen);
    lv_style_init(&startHanlde->styles.screenstyle);
    lv_style_reset(&startHanlde->styles.screenstyle);
    
    lv_style_set_x(&startHanlde->styles.screenstyle, 0);
    lv_style_set_y(&startHanlde->styles.screenstyle, 0);

    lv_style_set_width(&startHanlde->styles.screenstyle,SSD1306_WEIGHT);
    lv_style_set_height(&startHanlde->styles.screenstyle,SSD1306_HEIGHT);
    lv_style_set_bg_opa(&startHanlde->styles.screenstyle, LV_OPA_COVER); 
    lv_style_set_bg_color(&startHanlde->styles.screenstyle, lv_color_black());

    lv_style_set_pad_bottom(&startHanlde->styles.screenstyle, 0);
    lv_style_set_pad_left(&startHanlde->styles.screenstyle, 0);
    lv_style_set_pad_right(&startHanlde->styles.screenstyle, 0);
    lv_style_set_pad_row(&startHanlde->styles.screenstyle, 0);
    lv_style_set_pad_column(&startHanlde->styles.screenstyle, 0);

    lv_style_set_outline_pad(&startHanlde->styles.screenstyle, 0);
    lv_style_set_border_width(&startHanlde->styles.screenstyle, 0);

    lv_style_set_text_color(&startHanlde->styles.screenstyle, lv_color_white());
    lv_obj_add_style(startHanlde->resources.screen,&startHanlde->styles.screenstyle,0); 
    lv_scr_load(startHanlde->resources.screen);


    startHanlde->resources.statusBar = lv_bar_create(startHanlde->resources.screen);

    lv_style_init(&startHanlde->styles.status_bar_bg);
    lv_style_set_border_color(&startHanlde->styles.status_bar_bg, lv_palette_main(LV_PALETTE_BLUE));
    lv_style_set_border_width(&startHanlde->styles.status_bar_bg, 1);
    lv_style_set_pad_all(&startHanlde->styles.status_bar_bg, 2); /*To make the indicator smaller*/
    lv_style_set_radius(&startHanlde->styles.status_bar_bg, 5);

    lv_style_init(&startHanlde->styles.status_bar_indic);
    lv_style_set_bg_opa(&startHanlde->styles.status_bar_indic, LV_OPA_COVER);
    lv_style_set_bg_color(&startHanlde->styles.status_bar_indic, lv_palette_main(LV_PALETTE_BLUE));
    lv_style_set_radius(&startHanlde->styles.status_bar_indic, 4);

    lv_obj_remove_style_all(startHanlde->resources.statusBar);  /*To have a clean start*/
    lv_obj_add_style(startHanlde->resources.statusBar, &startHanlde->styles.status_bar_bg, 0);
    lv_obj_add_style(startHanlde->resources.statusBar, &startHanlde->styles.status_bar_indic, LV_PART_INDICATOR);

    lv_obj_set_size(startHanlde->resources.statusBar, 80, 12);
    lv_bar_set_range(startHanlde->resources.statusBar, 0, 10000);
    lv_obj_center(startHanlde->resources.statusBar);

    return 0;
}


uint32_t startPage_Exit(uint32_t arg)
{
    if(startHanlde)
    {
        lv_obj_del(startHanlde->resources.screen);
        free(startHanlde);
        startHanlde = 0;
    }
    return 0;
}



void showStatusBar()
{
    static int dd=0;
    dd += 100;
    if(dd >= 10000)
    {
        oledMessage msg;
        msg.source_page = OLED_PAGE_Start;
        msg.data.start.start_finish = 1;
        oledSendMessage(&msg);
    }
    else
    {
        lv_bar_set_value(startHanlde->resources.statusBar, dd, LV_ANIM_OFF);
    }  
}


uint32_t startPage_Process(uint32_t arg)
{
    switch(startHanlde->data.fsm)
    {
        case 0:
            startHanlde->data.fsm = 1;
        break;

        case 1:
            //set status bar val
            showStatusBar();
        break;
    }

    return 0;
}


